package com.lxk;



import java.util.*;

/**
 * @author 柳新宽
 * @create 2022-03-31  15:00
 * @describe
 */
public class FilmManager {
    //key存储的是影院信息   value存储的是一个集合   集合中存储的是影片信息
    HashMap<Cinema, List<Film>> map = new HashMap();
    //影片的编号
    Integer filmId = 1006;
    public void init(){
        //需求: 使用HashMap存储影院信息和对应的影片信息
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.YEAR,-4);
        instance.add(Calendar.MONTH,4);
        Date filmTime1 = instance.getTime();
        Film film1 = new Film(1001,"蝴蝶效应","惊悚,科幻","90分钟","林立,冰冰","这是一个穿越时间的电影",filmTime1);
        Calendar instance2 = Calendar.getInstance();
        instance2.add(Calendar.YEAR,-2);
        instance2.add(Calendar.MONTH,-2);
        Date filmTime2 = instance2.getTime();
        Film film2 = new Film(1002,"长津湖","战争,热血","180分钟","吴京,易烊千玺","这是一个战争的影片",filmTime2);
        Calendar instance3 = Calendar.getInstance();
        instance3.add(Calendar.YEAR,-3);
        instance3.add(Calendar.MONTH,-1);
        Date filmTime3 = instance3.getTime();
        Film film3 = new Film(1003,"流浪地球","灾难,科幻","170分钟","吴京,吴孟达","这是一个地球没了逃跑的电影",filmTime3);
        ArrayList<Film> films = new ArrayList<>();
        films.add(film1);
        films.add(film2);
        films.add(film3);
        Studio studio1 = new Studio(2001,"花开富贵厅",50);
        Studio studio2 = new Studio(2002,"粉色玫瑰厅",45);
        Studio studio3 = new Studio(2003,"荷塘月色厅",7);
        ArrayList<Studio> studios = new ArrayList<>();
        studios.add(studio1);
        studios.add(studio2);
        studios.add(studio3);
        Cinema cinema = new Cinema(3001,"夕阳红电影院","村头一号院",studios);
        map.put(cinema,films);

        Calendar instance4 = Calendar.getInstance();
        instance4.add(Calendar.YEAR,-10);
        instance4.add(Calendar.MONTH,-5);
        Date filmTime4 = instance4.getTime();
        Film film4 = new Film(1004,"迪迦奥特曼大电影","抗日,科幻","120分钟","大古,丽娜","这是一个抗日的大电影",filmTime4);
        Calendar instance5 = Calendar.getInstance();
        instance5.add(Calendar.YEAR,-3);
        instance5.add(Calendar.MONTH,-1);
        instance5.add(Calendar.DAY_OF_MONTH,-5);
        Date filmTime5 = instance5.getTime();
        Film film5 = new Film(1005,"大话西游","爱情,抗日","140分钟","吴孟达,周星驰","这是一个恶搞的电影",filmTime5);
        ArrayList<Film> films2 = new ArrayList<>();
        films2.add(film4);
        films2.add(film5);
        films2.add(film3);

        Studio studio4 = new Studio(2004,"VIP厅",30);
        Studio studio5 = new Studio(2005,"SVIP厅",40);
        ArrayList<Studio> studios2 = new ArrayList<>();
        studios2.add(studio4);
        studios2.add(studio5);
        Cinema cinema2 = new Cinema(3002,"万达影院","万达广场",studios2);
        map.put(cinema2,films2);
    }

    //展示影院的信息 key
    public void showAllCinema(){
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            System.out.println(cinema);
        }
    }
    //展示旗下影院放映的影片信息
    public HashSet<Film> showAllFilm(){
        Set<Cinema> cinemas = map.keySet(); //key的集合
        //建立HashSet来对重复的电影进行去重
        HashSet<Film> set = new HashSet<>();
        for (Cinema cinema : cinemas) {
            List<Film> films = map.get(cinema);
            for (Film film : films) {
                //System.out.println(film);//把所有的影院的所有的在映电影全部展示
                set.add(film);
            }
        }//所有的影片遍历结束并且全部放入到set中
        return set;
    }//方法结束
    //展示所有的影院名称
    public List<String> findAllCinemaName(){
        ArrayList<String> cinemaNames = new ArrayList<>();
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            cinemaNames.add(cinema.getCinemaName());
        }
        return cinemaNames;
    }
    //根据影院的名称查看旗下放映的影片
    public void showFilmByCinema(String cinemaName){
        Cinema cinema = getCinemaByName(cinemaName);
        if(cinema!=null){
            List<Film> films = map.get(cinema);
            for (Film film : films) {
                System.out.println(film);
            }
        }else{
            System.out.println("此影院不存在");
        }
    }
    //根据影院的名字获取对应的对象
    public Cinema getCinemaByName(String cinemaName){
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            if(cinemaName.equals(cinema.getCinemaName())){
                return cinema;
            }
        }
        return null;
    }
    //判断影片是否存在
    public boolean existsFilm(String filmName){
        //遍历所有的影片 进行比对
       /* 考虑到影片上映期间 多家影院的重复了从问题 所以此解决方法抛弃
       Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            List<Film> films = map.get(cinema);
            for (Film film : films) {
            }
        }*/
        HashSet<Film> set = showAllFilm();//电影 已经去重的
        for (Film film : set) {
            if(filmName.equals(film.getFilmName())){
                return false;
            }
        }
        return true;
    }
    //根据影院的名称上映新影片
    public void addFilmAndPublication(Film addFilm,String publicationCinemaNames){
        addFilm.setFilmId(filmId);
        filmId++;
        // n种可能
        // publicationCinemaNames= 万达影院 or 夕阳红影院  or  万达影院,夕阳红影院 or all
        if("all".equals(publicationCinemaNames)){
            //所有的电影院都上映
            Set<Cinema> cinemas = map.keySet();
            for (Cinema cinema : cinemas) {
                map.get(cinema).add(addFilm);
            }
        }else{
            String[] cinemaNameArray = publicationCinemaNames.split(",");
            // 1到n种
            for (String cinemaName : cinemaNameArray) {
                //每一次循环进来代表一个影院的名称
                Cinema cinema = getCinemaByName(cinemaName); // Cinema对象就是hashMap的key
                map.get(cinema).add(addFilm);
            }
        }
    }
    //根据电影名称获取正在放映的影院
    public List<String> getCinemaByFilmName(String filmName){
        ArrayList<String> list = new ArrayList<>();
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            List<Film> films = map.get(cinema);
            for (Film film : films) {
                if(filmName.equals(film.getFilmName())){
                    list.add(cinema.getCinemaName());//把影院的名称加进去
                    break;
                }
            }
        }
        return list;
    }
    //根据电影名和影院下架电影
    public void deleteFilmByCinema(String deleteFilmName,String cinemaName){
        Film film = getFilmByName(deleteFilmName);
        if("all".equals(cinemaName)){
            Set<Cinema> cinemas = map.keySet();
            for (Cinema cinema : cinemas) {
                map.get(cinema).remove(film);
            }
        }else{
            String[] cinemaNameArray = cinemaName.split(",");
            for (String cinemaName2 : cinemaNameArray) {
                Cinema cinema = getCinemaByName(cinemaName2); // Cinema对象就是hashMap的key
                map.get(cinema).remove(film);
            }
        }
    }

    //根据电影的名称获取电影的对象
    public Film getFilmByName(String filmName){
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            List<Film> films = map.get(cinema);
            for (Film film : films) {
                if(filmName.equals(film.getFilmName())){
                    return film;
                }
            }
        }
        return null;
    }
    //获取所有的电影题材
    public Set<String> getAllType(){
        HashSet<String> setOlder = new HashSet<>();
        HashSet<String> setNew = new HashSet<>();
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            List<Film> films = map.get(cinema);
            for (Film film : films) {
                setNew.add(film.getFilmType());//第一轮的去重
            }
        }//循环结束
        //setNew = {"惊悚,科幻","战争,热血","灾难,科幻","抗日,科幻","爱情,抗日"}
        for (String s : setNew) {
            String[] split = s.split(",");
            for (String s1 : split) {
                setOlder.add(s1);
            }
        }
        return setOlder;
    }
    //根据题材查询电影
    public List<Film> getFilmByType(String type){
        HashSet<Film> films = showAllFilm();
        ArrayList<Film> list = new ArrayList<>();
        for (Film film : films) {
            //"惊悚,科幻"
            int i = film.getFilmType().indexOf(type);
            if(i!=-1){
                list.add(film);
            }
        }
        return list;
    }
}
